<?php
/**
 *
 * @author Chen HuiYun <11chenhuiyun@gmail.com>
 * @copyright Copyright &copy
	{
	} 2012-2020 MyyTaijii Software LLC
 */

/**
 * 
 * @author Chen HuiYun
 */
class BRouteBeanArray extends BRouteBean
{
	private $_routeData = array();
	
	public function __construct()
	{
		$this->_routeData = array();
	}

	public function getAction($index)
	{
		return $this->getItem($index, 'action');
	}

	public function getData($index)
	{
		return $this->getItem($index, 'data');
	}

	public function getMainObj($index)
	{
	    $objItem = $this->getItemByIndex($index, 'obj');
	    return ($objItem && isset($objItem['main'])) ? trim($objItem['main']) : '';
	}

	public function getRule($index)
	{
		return $this->getItem($index, 'rule');
	}

	public function getSubObj($index)
	{
		$objItem = $this->getItemByIndex($index, 'obj');
	    return ($objItem && isset($objItem['sub'])) ? trim($objItem['sub']) : '';
	}

	public function getType($index)
	{
		return $this->getItem($index, 'type');
	}

	public function setAction($index, $newAction)
	{
		return $this->setItem($index, $newAction, 'action');
	}

	public function setData($index, $newData)
	{
		return $this->setItem($index, $newData, 'ctrl');
	}

	public function setMainObj($index, $newMainObj)
	{
		$objItem = $this->getItemByIndex($index, 'obj');
		if (!$objItem) {
			return false;
		}
		
		$objItem['main'] = $newMainObj;
		return $this->setItem($index, $objItem, 'obj');
	}

	public function setRule($index, $newRule)
	{
		return $this->setItem($index, $newRule, 'ctrl');
	}

	public function setSubObj($index, $newSubObj)
	{
		$objItem = $this->getItemByIndex($index, 'obj');
		if (!$objItem) {
			return false;
		}
		
		$objItem['sub'] = $newSubObj;
		return $this->setItem($index, $objItem, 'obj');
	}

	public function setType($index, $newType)
	{
		return $this->setItem($index, $newType, 'ctrl');
	}

	public function size()
	{
		return count($this->_routeData);
	}
	
	public function unshiftItem()
	{
		$item = array(
			'data'=>'',
			'ctrl'=>'',
			'type'=>'',
			'action'=>'',
			'rule'=>'',
			'obj'=>array('main'=>'', 'sub'=>'')
		);
		$routeData = $this->_routeData;
		array_unshift($routeData, $item);
		$this->_routeData = $routeData;
	}
	
	public function shiftItem()
	{
		$routeData = $this->_routeData;
		array_shift($routeData, $routeData);
		$this->_routeData = $routeData;
	}
	
	private function getItem($index, $tag)
	{
	    if (!is_integer($index) || ($index <= -1)) {
	    	return '';
	    }
	     
	    $item = $this->getItemByIndex($index);
	    return ($item && isset($item[$tag])) ? $item[$tag] : '';
	}
	
	private function getItemByIndex($index)
	{
		if (!is_integer($index) || ($index <= -1)) {
	    	return '';
	    }
	    
	    $routeData = $this->_routeData;
	    return ($routeData && isset($routeData[$index])) ? $routeData[$index] : '';
	}
	
	private function setItem($index, $value, $tag)
	{
		if (!is_integer($index) || ($index <= -1) || !$value) {
	    	return false;
	    }
	    
	    $routeData = $this->_routeData;
	    if ($routeData && isset($routeData[$index]) && isset($routeData[$index][$tag])) {
	    	$routeData[$index][$tag] = $value;
	    	$this->_routeData = $routeData;
	    	return true;
	    }
	    
	    return false;
	}
}
